﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Text;
using System.Threading;

public partial class BookingManage_ConfirmSendList : BasePage
{
    IBNBookingHandler bookingManager = new BNBookingHandler();
    DataBase db = new DataBase();
    string language = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        language = Thread.CurrentThread.CurrentUICulture.ToString().ToLower();

        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!strPermission.Contains("1046"))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }

            }
           
            dataBind();
        }
    }

    private void dataBind()
    {
        string sqlStr = @"SELECT bn.WinStart,bn.WinClose,dtl.*,client.CNShortName as Whouse, 
                            client.HKShortName as HKWhouse, client.ENGShortName as ENGWhouse,
                            shipper.CNShortName as Shipper, shipper.HKShortName as HKShipper, shipper.ENGShortName as ENGShipper,
                            vendor.CNShortName as Vendor, vendor.HKShortName as HKVendor, vendor.ENGShortName as ENGVendor
                        FROM BN_BookingNote bn INNER JOIN
                            BN_BookingNoteDtl dtl ON bn.PKID = dtl.BookingNoteID LEFT JOIN 
                            Sy_Client client ON dtl.WhouseID = client.PKID LEFT JOIN
                            Sy_Client shipper ON bn.ShipperID = shipper.PKID LEFT JOIN
                            Sy_Client vendor ON dtl.VendorID = vendor.PKID
                        WHERE dtl.PKID in (" + Request["chkSelected"].ToString() + ")";
        SqlParameter[] parms = null;
        string clause = string.Empty;
        //ProcessParms(ref clause, ref parms);
        sqlStr += clause;

        DataTable dt = bookingManager.GetList(sqlStr, parms);
        DataView view = dt.DefaultView;
        //view.Sort = ViewState["OrderBy"] + " " + ViewState["Accending"];
        gvPendingList.DataSource = view;
        gvPendingList.DataBind();
        
    }

    protected void gvPendingList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblShipper = (Label)e.Row.FindControl("lblShipper");
            Label lblHKShipper = (Label)e.Row.FindControl("lblHKShipper");
            Label lblENGShipper = (Label)e.Row.FindControl("lblENGShipper");

            Label lblWhouse = (Label)e.Row.FindControl("lblWhouse");
            Label lblHKWhouse = (Label)e.Row.FindControl("lblHKWhouse");
            Label lblENGWhouse = (Label)e.Row.FindControl("lblENGWhouse");

            Label lblVendor = (Label)e.Row.FindControl("lblVendor");
            Label lblHKVendor = (Label)e.Row.FindControl("lblHKVendor");
            Label lblENGVendor = (Label)e.Row.FindControl("lblENGVendor");

            if ("en".Equals(language))
            {
                lblShipper.Visible = false;
                lblHKShipper.Visible = false;
                lblENGShipper.Visible = true;

                lblWhouse.Visible = false;
                lblHKWhouse.Visible = false;
                lblENGWhouse.Visible = true;

                lblVendor.Visible = false;
                lblHKVendor.Visible = false;
                lblENGVendor.Visible = true;

            }
            else if ("zh-hk".Equals(language))
            {
                lblShipper.Visible = false;
                lblHKShipper.Visible = true;
                lblENGShipper.Visible = false;

                lblWhouse.Visible = false;
                lblHKWhouse.Visible = true;
                lblENGWhouse.Visible = false;

                lblVendor.Visible = false;
                lblHKVendor.Visible = true;
                lblENGVendor.Visible = false;
            }
            else
            {
                lblShipper.Visible = true;
                lblHKShipper.Visible = false;
                lblENGShipper.Visible = false;

                lblWhouse.Visible = true;
                lblHKWhouse.Visible = false;
                lblENGWhouse.Visible = false;

                lblVendor.Visible = true;
                lblHKVendor.Visible = false;
                lblENGVendor.Visible = false;
            }
        }
    }

    private void ProcessParms(ref string sqlstr, ref SqlParameter[] parms)
    {
        List<SqlParameter> list = new List<SqlParameter>();

        list.Add(new SqlParameter("@IDS", Request["chkSelected"].ToString()));
        parms = list.ToArray();
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (Request["chkSelected"] != null)
        {
            lblError.Visible = false;
            string ids = Request["chkSelected"].ToString();
            
            // 检查托运单状态和详细列表状态是否有改变
            string sqlStr = "SELECT distinct bn.PKID FROM BN_BookingNote bn" +
                            " inner join BN_BookingNoteDtl dtl on bn.PKID = dtl.BookingNoteID" + 
                            " WHERE bn.Status != " + HK2009.Common.Enums.BookingNoteStatus.CHECK.ToString("d") +
                            " and dtl.PKID in (" + ids + ")";
            SqlParameter[] parms = new SqlParameter[1];
            parms[0] = new SqlParameter("@IDS", Request["chkSelected"]);
            DataTable dt = bookingManager.GetList(sqlStr, parms);
            if (dt.Rows.Count > 0)
            {
                Jscript.AlertAndRedirect(Resources.Messages.WrongStatus, "PendingList.aspx", this.Page);
                return;
            }

            sqlStr = "SELECT * FROM BN_BookingNoteDtl WHERE PKID IN (" + ids + ") and Status not in(" + HK2009.Common.Enums.BookingNoteDtlStatus.NEW.ToString("d") + "," + HK2009.Common.Enums.BookingNoteDtlStatus.CANCELBOOKING.ToString("d") + ")";
            parms[0] = new SqlParameter("@IDS", Request["chkSelected"]);
            dt = bookingManager.GetList(sqlStr, parms);
            if (dt.Rows.Count > 0)
            {
                Jscript.AlertAndRedirect(Resources.Messages.WrongStatus + "--detail", "PendingList.aspx", this.Page);
                return;
            }

            List<SqlParameter> list = new List<SqlParameter>();
            sqlStr = @"SELECT TOP 1 @VendorID = VendorID FROM BN_BookingNoteDtl WHERE PKID IN (" + ids + @");

                        INSERT INTO BN_Booking(BookingNO,Status,VendorID,BookingUser,BookingDate)
                        VALUES(dbo.Fn_NextBookingNO(), @Status, @VendorID, @BookingUser, GETDATE());
                        SET @ReturnValue=@@Identity;";

            sqlStr += @"INSERT INTO BN_BookingDtl(BookingID,BookingNoteDtlID,ShipperID,SONO,PNNO,WinStart,
                        WinClose, JobDate,DeliveryDate,WhouseID,FCLLCL,ItemNO,PCS,CTN,GrossWT,VOL,Remark,PlaceID)
                        SELECT @ReturnValue, dtl.PKID,bn.ShipperID,dtl.SONO,dtl.PNNO,bn.WinStart,bn.WinClose,
                        dtl.JobDate,dtl.DeliveryDate,dtl.WhouseID,dtl.FCLLCL,dtl.ItemNO,dtl.PCS,dtl.CTN,
                        dtl.GrossWT,dtl.VOL,dtl.Remark, dtl.PlaceID FROM BN_BookingNote bn
                        INNER JOIN BN_BookingNoteDtl dtl ON bn.PKID = dtl.BookingNoteID
                        WHERE dtl.PKID IN (" + ids + @");
                        UPDATE BN_BookingNoteDtl SET Status = @DtlStatus, BookingDate = GETDATE(), BookingUser = @BookingUser
                        WHERE PKID IN (" + ids + ");";

            list.Add(new SqlParameter("@Status", HK2009.Common.Enums.BookingNoteStatus.NEW.ToString("d")));
            list.Add(new SqlParameter("@DtlStatus", HK2009.Common.Enums.BookingNoteDtlStatus.BOOKING.ToString("d")));
            //list.Add(new SqlParameter("@VendorID", int.Parse(Session["LoginUserClientID"].ToString())));
            list.Add(new SqlParameter("@VendorID", "00000000000000"));
            list.Add(new SqlParameter("@BookingUser", int.Parse(Session["UserID"].ToString())));
            list.Add(new SqlParameter("@IDS", Request["chkSelected"]));

            parms = list.ToArray();
            try
            {
                int bookingID = 0;
                if (bookingManager.BookingAdd(sqlStr, parms, ref bookingID))
                {
                    Jscript.AlertAndRedirect(Resources.Messages.SendSuccess, "BookingDtl.aspx?id=" + bookingID + "&rtn=PendingList.aspx", this.Page);
                }
            }
            catch { Jscript.Alert(Resources.Messages.SendFailed, this.Page); }
        }
        else
        {
            lblError.Visible = true;
        }
    }


    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("PendingList.aspx");
    }

}
